home *** CD-ROM | disk | FTP | other *** search
/ Creative Computers / Creative Computers CD-ROM, Volume 1 (Legendary Design Technologies, Inc.)(1994).iso / shareware / telecom / term_4.1 / extras / hydracom / hydracom-source.lha / fmisc.c < prev    next >
C/C++ Source or Header  |  1994-07-02  |  9KB  |  270 lines

  1. /*=============================================================================
  2.  
  3.                               HydraCom Version 1.00
  4.  
  5.                          A sample implementation of the
  6.                    HYDRA Bi-Directional File Transfer Protocol
  7.  
  8.                              HydraCom was written by
  9.                    Arjen G. Lentz, LENTZ SOFTWARE-DEVELOPMENT
  10.                   COPYRIGHT (C) 1991-1993; ALL RIGHTS RESERVED
  11.  
  12.                        The HYDRA protocol was designed by
  13.                  Arjen G. Lentz, LENTZ SOFTWARE-DEVELOPMENT and
  14.                             Joaquim H. Homrighausen
  15.                   COPYRIGHT (C) 1991-1993; ALL RIGHTS RESERVED
  16.  
  17.  
  18.   Revision history:
  19.   06 Sep 1991 - (AGL) First tryout
  20.   .. ... .... - Internal development
  21.   11 Jan 1993 - HydraCom version 1.00, Hydra revision 001 (01 Dec 1992)
  22.  
  23.  
  24.   For complete details of the Hydra and HydraCom licensing restrictions,
  25.   please refer to the license agreements which are published in their entirety
  26.   in HYDRACOM.C and LICENSE.DOC, and also contained in the documentation file
  27.   HYDRACOM.DOC
  28.  
  29.   Use of this file is subject to the restrictions contained in the Hydra and
  30.   HydraCom licensing agreements. If you do not find the text of this agreement
  31.   in any of the aforementioned files, or if you do not have these files, you
  32.   should immediately contact LENTZ SOFTWARE-DEVELOPMENT and/or Joaquim
  33.   Homrighausen at one of the addresses listed below. In no event should you
  34.   proceed to use this file without having accepted the terms of the Hydra and
  35.   HydraCom licensing agreements, or such other agreement as you are able to
  36.   reach with LENTZ SOFTWARE-DEVELOMENT and Joaquim Homrighausen.
  37.  
  38.  
  39.   Hydra protocol design and HydraCom driver:         Hydra protocol design:
  40.   Arjen G. Lentz                                     Joaquim H. Homrighausen
  41.   LENTZ SOFTWARE-DEVELOPMENT                         389, route d'Arlon
  42.   Langegracht 7B                                     L-8011 Strassen
  43.   3811 BT  Amersfoort                                Luxembourg
  44.   The Netherlands
  45.   FidoNet 2:283/512, AINEX-BBS +31-33-633916         FidoNet 2:270/17
  46.   arjen_lentz@f512.n283.z2.fidonet.org               joho@ae.lu
  47.  
  48.   Please feel free to contact us at any time to share your comments about our
  49.   software and/or licensing policies.
  50.  
  51. =============================================================================*/
  52.  
  53. #include "hydracom.h"
  54.  
  55. #ifdef AMIGA
  56. #define NAMELEN  32    /* OLSEN */
  57. #else
  58. #define NAMELEN  13
  59. #endif
  60.  
  61. #define LINELEN  64
  62.  
  63.  
  64. static char xfer_log[PATHLEN];
  65. static boolean xfer_logged;
  66. static char xfer_pathname[PATHLEN];
  67. static char xfer_real[NAMELEN],
  68.             xfer_temp[NAMELEN];
  69. static long xfer_fsize,
  70.             xfer_ftime;
  71.  
  72.  
  73. void unique_name (char *pathname)
  74. {
  75.         static char *suffix = ".000";
  76.         register char *p;
  77.         register int   n;
  78.  
  79.         if (fexist(pathname)) {
  80.            p = pathname;
  81.            while (*p && *p!='.') p++;
  82.            for (n=0; n<4; n++) {
  83.                if (!*p) {
  84.                   *p     = suffix[n];
  85.                   *(++p) = '\0';
  86.                }
  87.                else
  88.                   p++;
  89.            }
  90.  
  91.            while (fexist(pathname)) {
  92.                  p = pathname + ((int) strlen(pathname)) - 1;
  93.                  if (!isdigit(*p))
  94.                     *p = '0';
  95.                  else {
  96.                     for (n=3; n--;) {
  97.                         if (!isdigit(*p)) *p = '0';
  98.                         if (++(*p) <= '9') break;
  99.                         else               *p-- = '0';
  100.                     }/*for*/
  101.                  }
  102.            }/*while(exist)*/
  103.         }/*if(exist)*/
  104. }/*unique_name()*/
  105.  
  106.  
  107. char *xfer_init (char *fname, long fsize, long ftime)
  108. {
  109.         char  linebuf[LINELEN + 1];
  110.         char  bad_real[NAMELEN],
  111.               bad_temp[NAMELEN];
  112.         long  bad_fsize,
  113.               bad_ftime;
  114.         char *p;
  115.         FILE *fp;
  116.  
  117.         if (single_done)
  118.            return (NULL);
  119.  
  120.         strcpy(xfer_real,fname);
  121.         xfer_fsize = fsize;
  122.         xfer_ftime = ftime;
  123.  
  124.         mergepath(xfer_pathname,download,xfer_real);
  125.         if (fexist(xfer_pathname)) {
  126.            struct stat f;
  127.  
  128.            stat(xfer_pathname,&f);
  129.            if (xfer_fsize == f.st_size && xfer_ftime == f.st_mtime)
  130.               return (NULL);                            /* already have file */
  131.         }
  132.  
  133.         mergepath(xfer_log,download,"BAD-XFER.LOG");
  134.  
  135.         if ((fp = sfopen(xfer_log,"rt",DENY_WRITE)) != NULL) {
  136.            while (fgets(linebuf,LINELEN,fp)) {
  137.                  sscanf(linebuf,"%s %s %ld %lo",
  138.                                 bad_real, bad_temp, &bad_fsize, &bad_ftime);
  139.                  if (!strcmp(xfer_real,bad_real) &&
  140.                      xfer_fsize == bad_fsize && xfer_ftime == bad_ftime) {
  141.                     mergepath(xfer_pathname,download,bad_temp);
  142.                     if (fexist(xfer_pathname)) {
  143.                        fclose(fp);
  144.                        strcpy(xfer_temp,bad_temp);
  145.  
  146.                        xfer_logged = true;
  147.                        return (xfer_pathname);
  148.                     }
  149.                  }
  150.            }
  151.  
  152.            fclose(fp);
  153.         }
  154.  
  155.         strcpy(xfer_pathname,download);
  156.         p = xfer_pathname + ((int) strlen(xfer_pathname));
  157.         strcat(xfer_pathname,"BAD-XFER.000");
  158.         unique_name(xfer_pathname);
  159.         strcpy(xfer_temp,p);
  160.  
  161.         xfer_logged = false;
  162.         return (xfer_pathname);
  163. }/*xfer_init()*/
  164.  
  165.  
  166. boolean xfer_bad (void)
  167. {
  168.         struct stat f;
  169.         FILE *fp;
  170.         int n;
  171.  
  172.         if (single_file[0])
  173.            single_done = true;
  174.  
  175.         if (xfer_logged)                        /* Already a logged bad-xfer */
  176.            return (true);
  177.  
  178.         n = ((int) strlen(xfer_real)) - 1;
  179.         if (n > 3 &&
  180.             (!strcmp(&xfer_real[n-3],".PKT") || !strcmp(&xfer_real[n-3],".REQ"))) {
  181.            xfer_del();
  182.            return (false);                      /* don't recover .PKT / .REQ */
  183.         }
  184.  
  185.         stat(xfer_pathname,&f);
  186.         if (noresume || f.st_size < 1024L) {     /* not allowed/worth saving */
  187.            xfer_del();
  188.            return (false);
  189.         }
  190.  
  191.         if ((fp = sfopen(xfer_log,"at",DENY_WRITE)) != NULL) {
  192.            fprintf(fp,"%s %s %ld %lo\n",
  193.                      xfer_real, xfer_temp, xfer_fsize, xfer_ftime);
  194.            fclose(fp);
  195.  
  196.            return (true);                             /* bad-xfer logged now */
  197.         }
  198.  
  199.         xfer_del();
  200.         return (false);                             /* Couldn't log bad-xfer */
  201. }/*xfer_bad()*/
  202.  
  203.  
  204. char *xfer_okay (void)
  205. {
  206.         static char new_pathname[PATHLEN];
  207.         char *p;
  208.  
  209.         strcpy(new_pathname,download);
  210.         p = new_pathname + ((int) strlen(new_pathname));   /* start of fname */
  211.         if (single_file[0]) {
  212.            strcat(new_pathname,single_file);           /* add override fname */
  213.            single_done = true;
  214.         }
  215.         else {
  216.            strcat(new_pathname,xfer_real);                 /* add real fname */
  217.            unique_name(new_pathname);                      /* make it unique */
  218.         }
  219.         rename(xfer_pathname,new_pathname);           /* rename temp to real */
  220.         if (!nostamp && xfer_ftime)
  221.            setstamp(new_pathname,xfer_ftime);               /* set timestamp */
  222.  
  223.         if (xfer_logged)                         /* delete from bad-xfer log */
  224.            xfer_del();
  225.  
  226.         return (strcmp(p,xfer_real) ? p : NULL);              /* dup rename? */
  227. }
  228.  
  229.  
  230. void xfer_del (void)
  231. {
  232.         char  new_log[PATHLEN];
  233.         char  linebuf[LINELEN + 1];
  234.         char  bad_real[NAMELEN],
  235.               bad_temp[NAMELEN];
  236.         long  bad_fsize,
  237.               bad_ftime;
  238.         FILE *fp, *new_fp;
  239.         boolean left;
  240.  
  241.         if (fexist(xfer_pathname))
  242.            unlink(xfer_pathname);
  243.  
  244.         if ((fp = sfopen(xfer_log, "rt", DENY_WRITE)) != NULL) {
  245.            mergepath(new_log,download,"BAD-XFER.$$$");
  246.            if ((new_fp = sfopen(new_log, "wt", DENY_ALL)) != NULL) {
  247.               left = false;
  248.               while (fgets(linebuf,LINELEN,fp)) {
  249.                     sscanf(linebuf,"%s %s %ld %lo",
  250.                                    bad_real, bad_temp, &bad_fsize, &bad_ftime);
  251.